/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.shiep.aurora.calendar;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JLabel;
import javax.swing.JTextField;
import work02.w5.control.MyCalendar;
import work02.w5.control.Schedule;
import work02.w5.model.DateWithLunar;
import work02.w5.model.ScheduleModel;

/**
 *
 * @author Eric
 */
public class CalendarFrameAdapter {

    private Calendar cal = Calendar.getInstance();
    private int showedYear;
    private int showedMonth;
    MyCalendar myCal;
    Schedule schedule;
    private BackGround back;
    private ScheduleModel[] showingScheduleRecoder;
 
    public void IOC(BackGround back,
            MyCalendar myCal,
            Schedule schedule) {
        this.back = back;
        this.myCal = myCal;
        this.schedule = schedule;

    }

    /**
     * XXXX年XX月形式
     */
    public String getShowedTime() {
        return showedYear + "年" +( showedMonth+1) + "月";
    }

    public ScheduleModel getShowingScheduleModel(int index){
        return showingScheduleRecoder[index];
    }
    
    /**
     * 设置日历上显示的日程安排，目前仅为测试，需要变更
     *
     * @param ss
     */
    public void setCalendarSchedule(ScheduleModel... ss) {
        showingScheduleRecoder=ss;
        cal.setTime(new Date());
        back.noScheduleJLabel.setVisible(false);
        if (ss.length <= 0) {
            back.noScheduleJLabel.setVisible(true);
        }
            for (int i = 1; i <= 5; i++) {
               JLabel jt = null;
                try {
                    try {
                        jt = (JLabel) back.getClass().getDeclaredField("schedule" + i).get(back);
                    } catch (IllegalArgumentException | IllegalAccessException ex) {
                        Logger.getLogger(CalendarFrameAdapter.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } catch (NoSuchFieldException | SecurityException ex) {
                    Logger.getLogger(CalendarFrameAdapter.class.getName()).log(Level.SEVERE, null, ex);
                }
                jt.setVisible(false);
                if (i <= ss.length) {
                    jt.setVisible(true);
                    jt.setText(ss[i - 1].toString());
                }
        }
    }

    /**
     * 将日历的时间向下翻一月
     */
    public void setCalendarNextMonth() {
        if (showedMonth == 0 && showedYear == 0) {
            setCalendarPresentTime();
        }
        if (showedMonth == 11) {
            setCalendar(showedYear + 1, 0);
        } else {
            setCalendar(showedYear, showedMonth + 1);
        }
    }

    /**
     * 将日历的时间向上翻一月
     */
    public void setCalendarLastMonth() {
        if (showedMonth == 0 && showedYear == 0) {
            setCalendarPresentTime();
        }
        if (showedMonth == 0) {
            setCalendar(showedYear - 1, 11);
        } else {
            setCalendar(showedYear, showedMonth - 1);
        }
    }

    /**
     * 设置日历为目前时间
     */
    public void setCalendarPresentTime() {
        DateWithLunar present = myCal.getTodayDate();
        setCalendar(present.getTimeOfYear(), present.getTimeOfMonth());
    }

     public void refreshCalendar(){
          setCalendar(showedYear, showedMonth);
     }
    
    /**
     * 设置日历
     *
     * @param year >0
     * @param month 0-11
     */
    public void setCalendar(int year, int month) {
        /*
         * 设置当前年月属性
         */
        showedMonth = month;
        showedYear = year;
        DateWithLunar[] lastMonth, thisMonth, nextMonth;
        if (month == 0) {
            lastMonth = myCal.getAMonthDate(year - 1, 11);
            nextMonth = myCal.getAMonthDate(year, month + 1);
        } else if (month == 11) {
            lastMonth = myCal.getAMonthDate(year, month - 1);
            nextMonth = myCal.getAMonthDate(year + 1, 0);
        } else {
            lastMonth = myCal.getAMonthDate(year, month - 1);
            nextMonth = myCal.getAMonthDate(year, month + 1);
        }
        thisMonth = myCal.getAMonthDate(year, month);
        setCalendar(lastMonth, thisMonth, nextMonth);
    }

    private void setCalendar(DateWithLunar[] lastMonth, DateWithLunar[] thisMonth, DateWithLunar[] nextMonth) {
        DateWithLunar monthFirstDay = thisMonth[0];
        back.solarCalendarYearAndMonthJLabel.setText(monthFirstDay.getTimeOfYear() + "年 " + (monthFirstDay.getTimeOfMonth() + 1) + "月");
        back.lunarCalendarYearAndMonthJLabel.setText(monthFirstDay.getLunarYear() + monthFirstDay.getLunarMonth());
        /*
         *计算第一天在日历中位置的偏移 
         */
        int skewing = 0;
        switch (monthFirstDay.getTimeOfWeek()) {
            case Calendar.SUNDAY:
                skewing = 0;
                break;
            case Calendar.MONDAY:
                skewing = 1;
                break;
            case Calendar.TUESDAY:
                skewing = 2;
                break;
            case Calendar.WEDNESDAY:
                skewing = 3;
                break;
            case Calendar.THURSDAY:
                skewing = 4;
                break;
            case Calendar.FRIDAY:
                skewing = 5;
                break;
            case Calendar.SATURDAY:
                skewing = 6;
                break;
            default:
                skewing = 0;
                break;
        }
        /*
         *将一维的日期连接起来变成二维 
         */
        DateWithLunar[][] dates = new DateWithLunar[6][7];
        int lastMonthSkewing = lastMonth.length - skewing;
        int thisMonthCount = 0, nextMonthCount = 0;
        for (int i = 0; i < dates.length; i++) {
            for (int j = 0; j < dates[i].length; j++) {
                if (lastMonthSkewing < lastMonth.length) {
                    dates[i][j] = lastMonth[lastMonthSkewing];
                    lastMonthSkewing++;
                } else if (thisMonthCount < thisMonth.length) {
                    dates[i][j] = thisMonth[thisMonthCount];
                    thisMonthCount++;
                } else {
                    dates[i][j] = nextMonth[nextMonthCount];
                    nextMonthCount++;
                }
            }
        }
        setCalendar(dates);
    }

    private void setCalendar(DateWithLunar[][] dates) {
        DateWithLunar thisMonth = dates[1][1];
         DateWithLunar today=null;
        for (int i = 0; i < dates.length; i++) {
            for (int j = 0; j < dates[i].length; j++) {
                DateWithLunar date = dates[i][j];
                back.dates[i][j].setDate(dates[i][j]);
                boolean isWeekDay = myCal.isWeekDay(date);
                boolean hasSchedule = schedule.hasSchedule(myCal.getTheDayStart(date), myCal.getTheDayEnd(date));
                boolean isToday = myCal.isSameDay(date, myCal.getTodayDate());
                boolean isCurrentShowMonth = (myCal.comparedMonth(thisMonth, date) == 0);
                back.dates[i][j].setState(true, isWeekDay, isToday, hasSchedule, isCurrentShowMonth);
                if(isToday)
                    today=date;
            }
        }
        if(today!=null)
            /*
             *设置提醒面板内容 
             */
        setCalendarSchedule(schedule.getSchedule( myCal.getTheDayStart(today), myCal.getTheDayEnd(today)));
    }
}
